﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Login
{
    public partial class FormBase : Form
    {
        ToolStripMenuItem tsmi_Mi;

        private FormBase()
        {
            InitializeComponent();
        }

        public FormBase(ToolStripMenuItem tsmi)
        {
            tsmi_Mi = tsmi;
            InitializeComponent();
        }

        private void FormBase_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (Tag != null && Tag is ToolStripMenuItem)
            {
                ToolStripMenuItem miChild = Tag as ToolStripMenuItem;
                ToolStripMenuItem miParent = miChild.OwnerItem as ToolStripMenuItem;
                miParent.DropDownItems.Remove(miChild);
            }
            if (tsmi_Mi != null)
            {
                tsmi_Mi.Enabled = true;
            }
        }

        public BindingNavigator Navigator
        {
            get
            {
                return bindingNavigator;
            }
        }

        public bool ShowNavigator
        {
            set
            {
                bindingNavigator.Visible = value;
            }
        }

        public bool ShowDelBtn
        {
            set
            {
                tsbDel.Visible = value;
            }
        }

        public bool ShowCopyBtn
        {
            set
            {
                tsbCopy.Visible = value;
            }
        }

        private void tsbDel_Click(object sender, EventArgs e)
        {
            Delete();
        }

        protected virtual void Delete()
        {

        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        protected virtual void Save()
        {
            MessageBox.Show("Parent Save");
        }

        public string UserName
        {
            get
            {
                string rtn = System.AppDomain.CurrentDomain.GetData("username") == null ? "" : System.AppDomain.CurrentDomain.GetData("username").ToString();
                return rtn;
            }
        }

        protected DataTable GetDataTableByGrid(DataGridView dgv)
        {
            DataTable dt = null;
            if (dgv != null && dgv.DataSource != null)
            {
                if (dgv.DataSource is BindingSource)
                {
                    BindingSource bs = dgv.DataSource as BindingSource;
                    if (bs.DataSource is DataView)
                    {
                        dt = (bs.DataSource as DataView).Table;
                    }
                    else if (bs.DataSource is DataTable)
                    {
                        dt = bs.DataSource as DataTable;
                    }
                }
            }
            return dt;
        }

        protected BindingSource GetBindingSourceByGrid(DataGridView dgv)
        {
            BindingSource bs = null;
            if (dgv != null && dgv.DataSource != null)
            {
                if (dgv.DataSource is BindingSource)
                {
                    bs = dgv.DataSource as BindingSource;
                }
            }
            return bs;
        }
    }
}
